<?php
/**
 * Created by PhpStorm.
 * User: fanfz_000
 * Date: 2015/4/7 0007
 * Time: 19:42
 * 作用 ：后台管理
 */
class Root extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function captcha_image()
    {

        $this->load->helper('captcha');
        $vals = array(
            'word' => rand(1000, 9999),
            'img_path' => './public/captcha/',
            'img_url' => './../public/captcha/',
            'font_path' => './public/fonts/verdana.ttf',
            'img_width' => '90',
            'img_height' => 30,
            'expiration' => 7200,
            'id' => 'img_code',
            'alt' => '点击刷新'
        );

        $cap = create_captcha($vals);
        $image_data = array(
            'captcha_time' => $cap['time'],
            'ip_address' => $this->input->ip_address(),
            'word' => $cap['word']
        );
        $query = $this->db->insert_string('captcha', $image_data);
        $this->db->query($query);

        echo $cap['image'];
    }

    function login()
    {
        $this->load->helper('form');
        $this->load->view('root_login');
    }

    function captcha()
    {
        $expiration = time() - 7200; // 2小时限制
        $this->db->query("DELETE FROM captcha WHERE captcha_time < " . $expiration);

        // 然后再看是否有验证码存在:
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();

        echo $row->count;
    }
    function login_go()
    {
        $username = $this->input->post('user_name');
        $password = $this->input->post('user_pwd');
        if ($username == 'fanfzj' && $password == 'fanfzj') {
            $this->load->library('commonclass');
            $this->load->helper('cookie');
            $cookie = array(
                'name' => 'some_data',
                'value' => $this->commonclass->getRandChar(rand(0, 20)),
                'expire' => '800',
                'domain' => '.some-domain.com',
                'path' => '/',
                'prefix' => 'fzj_',
                'secure' => TRUE
            );
            $this->input->set_cookie($cookie);
            $this->load->library('encrypt');
            $this->load->library('session');
            $newdata = array(
                'username' => 'fanfzj',
                'email' => 'fanfzj@163.com',
                'random' => $this->encrypt->encode($this->commonclass->getRandChar(rand(0, 20))),
                'logged_in' => TRUE
            );
            $this->session->set_userdata($newdata);
            redirect('/Root/index/', 'refresh');
        } else {
            redirect('/Root/login/', 'refresh');
        }
    }
    //首页
    function index()
    {
        //$random = $this->input->cookie('some_data', TRUE);
        $this->load->library('session');
        $session_id = $this->session->userdata('session_id');
        $this->load->model('Ci_sessions_model', 'ci_sessions', TRUE);
        $tf = $this->ci_sessions->comparev_session($session_id);
        //echo $tf;
        if ($tf == 0) {
            //echo $tf;
            $this->load->helper(array('form', 'url'));
            $this->load->helper('url');
            redirect('/Root/login/', 'refresh');
        } else {
            //$data['tf']=$tf;
            //缓存60分钟更新一次
            //$this->output->cache(60);
            //模型操作，图片轮播图片列表
            $this->load->model('Backgroudurl_model', 'image', TRUE);
            $data['image_list'] = $this->image->show_image_list();

            //模型操作,显示关于我
            $this->load->model('Aboutme_model', 'about_me', TRUE);
            $data['about_me'] = $this->about_me->show_me_info();

            //模型操作,标签云
            $this->load->model('Label_model', 'label', TRUE);
            $data['label_list'] = $this->label->show_label_list();

            //模型操作,显示友情链接
            $this->load->model('Friendhref_model', 'friend_href', TRUE);
            $data['friend_list'] = $this->friend_href->show_friend_href_list();

            //页面加载
            $this->load->view('root_header', $data);
            $this->load->view('root');
        }
    }

    //更新关于我
    function reset_about_me()
    {
        $sina_href = $this->input->post('sina_href');
        $sina_title = $this->input->post('sina_title');
        $qq = $this->input->post('qq');
        $email = $this->input->post('email');
        $qq_zone_href = $this->input->post('qq_zone_href');
        $qq_zone_title = $this->input->post('qq_zone_title');
        $renren_href = $this->input->post('renren_href');
        $renren_title = $this->input->post('renren_title');
        $city = $this->input->post('city');
        $introduce_me = $this->input->post('introduce_me');
        $sign = $this->input->post('sign');
        $school = $this->input->post('school');
        $college = $this->input->post('college');
        $field = $this->input->post('field');

        $this->load->model('Aboutme_model', 'about_me', TRUE);
        $about_me = $this->about_me->upset_me_info($sina_href, $sina_title, $qq, $email, $qq_zone_href, $qq_zone_title, $renren_href, $renren_title, $introduce_me, $sign, $school, $college, $field, $city);
        echo $about_me;
    }

    //更新我的头像
    function do_upload()
    {
        $config['upload_path'] = './public/image';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['max_width'] = '3000';
        $config['max_height'] = '5000';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            echo $error;
        } else {
            $data = $this->upload->data();
            $this->load->model('aboutme_model', 'about_me', TRUE);
            $photo = $this->about_me->upload_photo("public/image/" . $data['file_name']);
            echo $photo;
        }
    }

    //显示我的头像
    function show_photo()
    {
        $this->load->model('aboutme_model', 'about_me', TRUE);
        $photo = $this->about_me->show_photo();
        echo json_encode($photo);
    }

    function backup()
    {
        // 加载数据库工具类
        $this->load->dbutil();

        // 备份整个数据库并将其赋值给一个变量
        $backup = $this->dbutil->backup();
        // 加载文件辅助函数并将文件写入你的服务器
        $this->load->helper('download');
        if (write_file(APP + '/public/file/mybackup.gz', $backup)) {
            echo json_encode(1);
        } else {
            echo json_encode(-1);
        }

        // 加载下载辅助函数并将文件发送到你的桌面
//        $this->load->helper('download');
//        force_download('mybackup.gz', $backup);
    }

    function sql()
    {
        // 加载数据库工具类
        $this->load->dbutil();
        $result = $this->dbutil->optimize_database();
        if ($result !== FALSE) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }
    }
}